package com.example.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.graduation.dto.AssetDO;
import com.graduation.dto.OrganizationDO;
import com.graduation.mapper.AssetCategoryMapper;
import com.graduation.mapper.AssetLabelMapper;
import com.graduation.mapper.AssetMapper;
import com.graduation.mapper.OrganizationMapper;
import com.graduation.pojo.*;
import com.graduation.pojo.OrganizationQuery;
import com.graduation.vo.AssetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@WebException
@RequestMapping("/asset")
public class AssetController extends BaseController<AssetDO, AssetVO> {

    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private AssetCategoryMapper assetCategoryMapper;
    @Autowired
    private AssetLabelMapper assetLabelMapper;
    @Autowired
    private OrganizationMapper organizationMapper;

    @ResponseBody
    @GetMapping("/get")
        public WebResult<AssetVO> get(Integer id) {
        return new WebResult<>(getVoByDo(assetMapper.selectById(id), AssetVO.class));
    }

    @ResponseBody
    @PostMapping("/page")
    public WebResult<PageResult<AssetVO>> page(AssetQuery query) {
        PageResult<AssetVO> result = new PageResult<>();
        if (Objects.nonNull(query.getOrgId())) {
            // 查询所有的子机构
            List<Integer> orgIdList = new ArrayList<>();
            orgIdList.add(query.getOrgId());
            buildChildOrg(orgIdList, query.getOrgId());
            query.setOrgIdList(orgIdList);
        }
        result.setRowCount(assetMapper.selectCount(query));
        result.setPageCount((result.getRowCount() - 1) / query.getPageSize() + 1);
        if (query.getPageNum() > result.getPageCount()) {
            query.setPageNum(result.getPageCount());
        }
        result.setPageNum(query.getPageNum());
        // 设置分页查询偏移量
        query.setLimit((query.getPageNum() - 1) * query.getPageSize());
        result.setEntityList(assetMapper.selectList(query).stream()
                .map(e -> getVoByDo(e, AssetVO.class))
                .collect(Collectors.toList()));
        return new WebResult<>(result);
    }

    private void buildChildOrg(List<Integer> orgIdList, Integer parentId) {
        OrganizationQuery query = new OrganizationQuery();
        query.setFatherOrg(parentId);
        // 查询子机构
        List<OrganizationDO> children = organizationMapper.selectList(query);
        for (OrganizationDO child: children) {
            if (!orgIdList.contains(child.getId())) {
                orgIdList.add(child.getId());
                buildChildOrg(orgIdList, child.getId());
            }
        }
    }

    @ResponseBody
    @PostMapping("/operate")
    public WebResult<?> operate(HttpServletRequest servletRequest, AssetRequest request) {
        AssetDO assetDO = getDoByRequest(request, AssetDO.class);
        if (Objects.isNull(assetDO.getId())) {
            assetDO.setCreateTime(LocalDateTime.now());
            assetDO.setUpdateTime(LocalDateTime.now());
            assetDO.setAssetNo(DateUtil.format(LocalDateTime.now(), "yyyyMMdd") + "_" + RandomUtil.randomInt(100, 999));
            assetMapper.insert(assetDO);
        } else {
            assetDO.setUpdateTime(LocalDateTime.now());
            assetMapper.updateById(assetDO);
        }
        return WebResult.success();
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public WebResult<?> delete(Integer id) {
        assetMapper.deleteById(id);
        return WebResult.success();
    }

    @RequestMapping("/to-asset")
    public String toAsset(Model model) {
        model.addAttribute("categoryList", assetCategoryMapper.selectList(new AssetCategoryQuery()));
        model.addAttribute("labelList", assetLabelMapper.selectList(new AssetLabelQuery()));
        model.addAttribute("organizationList", organizationMapper.selectList(new OrganizationQuery()));
        return "asset/assetAdmin";
    }

    @RequestMapping("/to-operate")
    public String toOperate(Model model, Integer id) {
        model.addAttribute("id", id);
        model.addAttribute("categoryList", assetCategoryMapper.selectList(new AssetCategoryQuery()));
        model.addAttribute("labelList", assetLabelMapper.selectList(new AssetLabelQuery()));
        model.addAttribute("organizationList", organizationMapper.selectList(new OrganizationQuery()));
        model.addAttribute("asset", Objects.isNull(id) ? new AssetDO(): assetMapper.selectById(id));
        return "asset/operateAsset";
    }
}
